//index.js
//获取应用实例
const app = getApp()
var time = require("../../utils/util.js");
import Dialog from '../../@vant/weapp/dialog/dialog';
Page({
  data: {
    show: true,
    overlay: false,
    checked: false,
    disabled: true,
    nickName: '',
    avatarUrl: '../../image/mypic.png',
    show1: false,
    phonenum: '',
    name: '',
    view: ''
  },
  onChange(event) {
    console.log(event)
    this.setData({
      checked: event.detail,
    });
    if(event.detail){
      this.setData({
        disabled: false
      });
    }else{
      this.setData({
        disabled: true
      });
    }
  },
  bindnext: function(){
    var that = this;
    if(that.data.checked){
      that.setData({
        show: false
      })
      console.log(wx.getStorageSync("nickName"))
      if(wx.getStorageSync("nickName") == ''){
        wx.getUserProfile({
          desc:'小程序',
          success: function(res) {
            console.log(res)
            if(res.errMsg == 'getUserProfile:ok'){
              that.setData({
                nickName: res.userInfo.nickName,
                avatarUrl: res.userInfo.avatarUrl,
              })
              wx.setStorageSync("nickName",res.userInfo.nickName);
              wx.setStorageSync("avatarUrl",res.userInfo.avatarUrl);
            }
          }
        })
      }else{
        that.setData({
          nickName: wx.getStorageSync("nickName"),
          avatarUrl: wx.getStorageSync("avatarUrl"),
        })
      }
      
    }   
  }, 
  bindnow: function(){
    var that = this;
     wx.navigateTo({
       url: '../nowlogs/nowlogs',
     })
  },
  bindafter: function(){
    var that = this;
     wx.navigateTo({
       url: '../logs/logs',
     })
  },
  bindsearch: function(){
    var that = this;
     wx.navigateTo({
       url: '../worklist/worklist',
     })
  },
  // bindsearch1: function(){
  //   var that = this;
  //    wx.navigateTo({
  //      url: '../traffic/traffic',
  //    })
  // },
  getPhoneNumber: function (e) {
    console.log(e);
    var that = this;
    console.log(e.detail.errMsg == "getPhoneNumber:ok");
    if (e.detail.errMsg == "getPhoneNumber:ok") {
      wx.request({
        url: time.httpurl + '/api/getPhone',
        data: {
          encryptedData: e.detail.encryptedData,
          iv: e.detail.iv,
          sessionKey: wx.getStorageSync("sessionKey"),
        },
        method: "post",
        success: function (res) {
          console.log(res);
          wx.setStorageSync("phone",res.data.data.phoneNumber);
          wx.redirectTo({
            url: '../register/register'
          })
        }
      })
    }
  },
   /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    var that = this;
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        wx.request({
          //获取openid接口  
          url: time.httpurl + '/api/login/' + res.code,
          method: 'GET',
          success: function (res) {
            console.log(res);     
            if(res.data.code == 601){
              that.setData({
                show1: true
              })     
              wx.setStorageSync("openId",res.data.data.openId);
              wx.setStorageSync("sessionKey",res.data.data.session_key);
            }else if(res.data.code == 200){        
              wx.setStorageSync("token",res.data.data.token);
              wx.setStorageSync("openId",res.data.data.openId);
              wx.request({
                //获取openid接口  
                url: time.httpurl + '/api/getUserInfo',
                method: 'GET',
                header: {
                  'AppType': 'AppType',
                  'Authorization': wx.getStorageSync("token"),
                },
                success: function (res) {
                  console.log(res);
                  if(res.data.code == 200){
                    that.setData({
                      phonenum: res.data.data.phone,
                      name: res.data.data.name,
                    })
                    
                  }
                }
              })
            }
          }
        })
      }
    })
    wx.request({
      //获取openid接口  
      url: time.httpurl + '/api/getUserTerms',
      method: 'GET',
      header: {
        'AppType': 'AppType',
        'Authorization': wx.getStorageSync("token"),
      },
      success: function (res) {
        console.log(res);
        if(res.data.code == 200){
          that.setData({
            show: true
          })
          that.setData({
            view: res.data.data.content.replace(/##/g, ' ')
          })
          
        }
      }
    })
  },
  unbundling: function(){
    Dialog.alert({
      message: '确定要解除绑定吗？',
    }).then(() => {
      wx.request({
        //获取openid接口  
        url: time.httpurl + '/api/removeBinding',
        method: 'POST',
        header: {
          'AppType': 'AppType',
          'Authorization': wx.getStorageSync("token"),
        },
        success: function (res) {
          console.log(res);
          if(res.data.code == 200){          
            wx.redirectTo({
              url: '../register/register'
            })
          }
        }
      })
    });
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },
 
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
   
  },
 
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },
 
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },
 
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },
 
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})
